package xhr;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;

import javax.security.auth.callback.Callback;

public class XhrProducer {
	public static void main(String[] args) throws Exception {
    	// 实例化消息生产者Producer
        DefaultMQProducer producer = new DefaultMQProducer("xhr_group_name");
    	// 设置NameServer的地址
    	producer.setNamesrvAddr("localhost:9876");
    	// 启动Producer实例
        producer.start();
		// 创建消息，并指定Topic，Tag和消息体
		Message msg = new Message("xhr_order_topic" /* Topic */,
				"xhr_employee_tag" /* Tag */,
				"异步消息".getBytes()
		);
		// 发送异步消息，用匿名内部类
		producer.send(msg, new SendCallback() {
			@Override
			public void onSuccess(SendResult sendResult) {
				System.out.println(sendResult);
			}

			@Override
			public void onException(Throwable throwable) {
				System.out.println(throwable.getMessage());
			}
		});
		// 通过sendResult返回消息是否成功送达
		System.out.printf("发送异步消息成功");
    	// 如果不再发送消息，关闭Producer实例。
//    	producer.shutdown();
    }
}